home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Cheat II / original / main.c < prev    next >
C/C++ Source or Header  |  1995-09-04  |  6KB  |  297 lines

  1. /*****
  2.  * main.c for cheat Account
  3.  *
  4.  *
  5.  *****/
  6.  
  7. #include <stdarg.h>
  8. #include "cheatMenus.h"
  9. #include "cheatrick.h"
  10. #include "cheatWindow.h"
  11. #include "ResRefs.h"
  12. #include "error.h"
  13. #include "main.h"
  14. #include "cheat.h"
  15. #include "cheatpreffile.h"
  16. #include "cheatwich.h"
  17. #include "cheatfile.h"
  18.  
  19. #define kbreathingroom 100000        // how many k we don't eat with our possibilities list
  20.  
  21. // some globals 'n shit
  22.  
  23.  
  24. char scratch[257];
  25. int gvalsize = 1, ghowfind = 0, shouldclearup = false;
  26. Ptr *gposs;
  27. long gmaxposs = 0, gcurposs = 0;
  28.  
  29. int timeToQuit = false;
  30. int gInBackground = false;
  31.  
  32. shortcut **cut;
  33. long gnumcuts = 0;
  34.  
  35. extern    Rect        dragRect;
  36.  
  37. preftype prefs;
  38.  
  39. void InitMacintosh(void);
  40. void HandleMouseDown (EventRecord    *theEvent);
  41. void HandleEvent(void);
  42.  
  43. /****
  44.  * InitMacintosh()
  45.  *
  46.  * Initialize all the managers & memory
  47.  *
  48.  ****/
  49.  
  50. void InitMacintosh(void)
  51.  
  52. {
  53.     MaxApplZone();
  54.     
  55.     InitGraf(&thePort);
  56.     InitFonts();
  57.     FlushEvents(everyEvent, 0);
  58.     InitWindows();
  59.     InitMenus();
  60.     TEInit();
  61.     InitDialogs(0L);
  62.     InitCursor();
  63.     
  64.     if (DoReadPrefs((Ptr) &prefs, sizeof(preftype)) == sizeof(preftype)) {
  65.     }
  66.     prefs.shouldpatchbutt = true;
  67. }
  68.  
  69.  
  70.     // allocate some memory
  71. static void initmemstuff(void)
  72. {
  73.     long l;
  74.     
  75.     allocmem();
  76.     l = MaxMem(0);
  77.     gmaxposs = (l - kbreathingroom)/4;
  78.     gposs = (Ptr *) NewPtr(4 * gmaxposs);
  79.     verify(gposs);
  80. }
  81.  
  82.  
  83. void HandleControlClicks (EventRecord    *theEvent, WindowPtr wp)
  84. {
  85.     int partcode, result, i;
  86.     ControlHandle which;
  87.     Point pt = theEvent->where;
  88.     Str255 str;
  89.     Boolean selected;
  90.     Cell cell = {0, 0}, newcell = {0, 0};
  91.     
  92.     SetPort(wp);
  93.     GlobalToLocal(&pt);        // must have click in local coords before dealing with cntls
  94.  
  95.     if (wp == cheatWindow) {
  96.         if (ghowfind == kfindinterrupt)
  97.             return;        // can't select a process in this case
  98.         selected = LGetSelect(true, &cell, plist);        // what cell they have selected before click
  99.         if (LClick(pt, 0, plist));
  100.             // make sure still same
  101.         if ((selected != LGetSelect(true, &newcell, plist)) || (selected && (cell.v != newcell.v)))
  102.             doStartOver();        // reset our data
  103.     }
  104.     else if (wp == sheetWindow) {
  105.         if (LClick(pt, theEvent->modifiers, slist))
  106.             doOpenCheat();
  107.         fixshortbuttz();
  108.     }
  109. }
  110.  
  111.  
  112. void HandleMouseDown (EventRecord    *theEvent)
  113. {
  114.     WindowPtr    theWindow;
  115.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  116.     
  117.     switch (windowCode)
  118.       {
  119.       case inSysWindow: 
  120.         SystemClick (theEvent, theWindow);
  121.         break;
  122.         
  123.       case inMenuBar:
  124.     //      AdjustMenus();
  125.         HandleMenu(MenuSelect(theEvent->where));
  126.         break;
  127.         
  128.       case inDrag:
  129.           if (theWindow == cheatWindow || theWindow == sheetWindow)
  130.             DragWindow(theWindow, theEvent->where, &dragRect);
  131.             break;
  132.             
  133.       case inContent:
  134.           if (theWindow == cheatWindow || theWindow == sheetWindow) {
  135.             if (theWindow != FrontWindow())
  136.               SelectWindow(theWindow);
  137.             else
  138.             HandleControlClicks(theEvent, theWindow);
  139.           }
  140.           break;
  141.           
  142.       case inGoAway:
  143.           if (theWindow == cheatWindow && 
  144.               TrackGoAway(cheatWindow, theEvent->where))
  145.           HideWindow(cheatWindow);
  146.             break;
  147.       }
  148. }
  149. /* end HandleMouseDown */
  150.  
  151.  
  152. /****
  153.  * HandleEvent()
  154.  *
  155.  *        The main event dispatcher. This routine should be called
  156.  *        repeatedly (it  handles only one event).
  157.  *
  158.  *****/
  159.  
  160. void HandleEvent(void)
  161.  
  162. {
  163.     WindowPtr    theWindow;
  164.     int            ok;
  165.     short item;
  166.     EventRecord    theEvent;
  167.     DialogPtr dp;
  168.     char c;
  169.     short devent;
  170.  
  171.     HiliteMenu(0);
  172.     SystemTask();        /* Handle desk accessories */
  173.     
  174.     ok = GetNextEvent (everyEvent, &theEvent);
  175.     devent = IsDialogEvent(&theEvent);
  176.     if (devent)
  177.         if (DialogSelect(&theEvent, &dp, &item))
  178.             if (dp == cheatWindow)
  179.                 switch (item) {
  180.                     case riSearchButt: searchButtonHit(); break;
  181.                     case riStartOverButt: doStartOver(); break;
  182.                     case riChangeButt: doChange(); break;
  183.                     case riSizeLong: case riSizeInt: case riSizeByte: doSizeChange(item); break;
  184.                     case riSelecting: case riInterrupting: doRuptOrSelect(item); break;
  185.                     default: break;
  186.                 }
  187.             else if (dp == sheetWindow)
  188.                 switch (item) {
  189.                     case riAddCheat: doAddCheat(); break;
  190.                     case riRemoveCheat: doRemoveCheat(); break;
  191.                     case riOpenCheat: doOpenCheat(); break;
  192.                     case riExportNew: doExportShortcut(knewfile); break;
  193.                     case riExportAppend: doExportShortcut(kappendnew); break;
  194.                     case riAppendToOld: doExportShortcut(koldfile); break;
  195.                     case riImport: doOpenFile(); break;
  196.                     default: break;
  197.                 }
  198.     if (ok)
  199.       switch (theEvent.what)
  200.         {
  201.         case mouseDown:
  202.             HandleMouseDown(&theEvent);
  203.             break;
  204.             
  205.         case keyDown: 
  206.         case autoKey:
  207.             if ((theEvent.modifiers & cmdKey) != 0)
  208.               {
  209.            //   AdjustMenus();
  210.          //          if (!devent)
  211.                   HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  212.               }
  213.             else {
  214.                 c = (char) (theEvent.message & charCodeMask);
  215.                 HandleKey(c);
  216.             }
  217.             break;
  218.             
  219.         case updateEvt:
  220.         //    BeginUpdate(cheatWindow);
  221.             theWindow = (WindowPtr) theEvent.message;
  222.             if (theWindow == cheatWindow)
  223.                 DrawCheatWindow();
  224.             else if (theWindow == sheetWindow)
  225.                 DrawSheetWindow();
  226.         //    EndUpdate(cheatWindow);
  227.             break;
  228.  
  229.         case osEvt:
  230.         /*    1.02 - must BitAND with 0x0FF to get only low byte */
  231.             switch ((theEvent.message >> 24) & 0x0FF) {        /* high byte of message */
  232.                 case suspendResumeMessage:        /* suspend/resume is also an activate/deactivate */
  233.                     gInBackground = (theEvent.message & resumeFlag) == 0;
  234.                     if (!gInBackground)
  235.                         InitCursor();
  236.                     break;
  237.             }
  238.             break;
  239.             
  240.         case activateEvt:
  241.             InvalRect(&cheatWindow->portRect);
  242.             InitCursor();
  243.             break;
  244.         }
  245. }
  246. /* end HandleEvent */
  247.  
  248. void checkclearup(void)
  249. {
  250.     if (shouldclearup) {
  251.         shouldclearup = false;
  252.         doclearup();
  253.         if (ghowfind)
  254.             installTMTask();        // reinstall task so more things can happen
  255.     }
  256. }
  257.  
  258.  
  259.     // a completely bogus function that calls some commands so that
  260.     // the code segment they reside in gets into memory, in prep for cheatwich
  261.     // otherwise, major crash (checking preload doesn't seem to help, but this works)
  262. static void loadthatseg(char *fmt, ...)
  263. {
  264.     va_list args;
  265.     int len;
  266.     char itch[200];
  267.     
  268.     va_start(args, fmt);
  269.     len = vsprintf(itch, fmt, args);
  270.     va_end(args);
  271. }
  272.  
  273.  
  274. main()
  275.  
  276. {
  277.     InitMacintosh();
  278.     SetUpMenus();
  279.     SetUpWindow();
  280.     OpenAppFile();
  281.     initmemstuff();
  282. //    doInstallTrap();
  283. //    installTMTask();
  284. //    cheatwich(0);
  285.     loadthatseg("hi there");
  286.     remembertraps();
  287.     
  288.     do {
  289.         HandleEvent();
  290.         if (!gInBackground)
  291.             refreshProcesses();        // self-regulates to only execute occasionally
  292.         checkclearup();
  293.     } while (!timeToQuit);
  294. //    doRemoveTrap();
  295.     removeTMTask();
  296. }
  297. /* end main */